
package com.waleipt.api.es.tcptransport;

import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportException;
import org.elasticsearch.transport.TransportResponseHandler;
import org.junit.Test;

import com.waleipt.api.es.tcptransport.message.UserRequest;
import com.waleipt.api.es.tcptransport.message.UserResponse;

/**
 * ES-TCPTransport 2 - 自定义ES协议解析
 * 
 * @author Administrator
 *
 */
public class Custom_protocol_test {

	@Test
	public void test() 
	{
		try {

			Server_Node server_node = new Server_Node(9300);
			server_node.regHandleRequest("action_1", UserRequest::new, (request, channel) -> {
				System.err.println(String.format("处理请求参数:id/name/age:%d/%s/%d", request.id, request.name, request.age));
				channel.sendResponse(new UserResponse(3, "199999"));
			});
			server_node.start();

			// 客户端链接服务端
			Client_Node client_node = new Client_Node(9301);
			client_node.start();
			client_node.connectToNode("10.100.33.155", 9300);
			long requestId = 42;
			String action = "action_1";
			UserRequest request = new UserRequest(1, "王五", 23);
			client_node.sendRequest(requestId, action, request, new TransportResponseHandler<UserResponse>() {

				@Override
				public UserResponse newInstance() {
					return new UserResponse();
				}

				@Override
				public void handleResponse(UserResponse response) {
					System.err.println(String.format("处理服务端返回结果:status/reason/:%d/%s", response.status, response.reason));
				}

				@Override
				public void handleException(TransportException exp) {
					// TODO Auto-generated method stub

				}

				@Override
				public String executor() {
					return ThreadPool.Names.SAME;
				}
			});

			Thread.sleep(5000);

		} catch (Throwable err) {
			err.printStackTrace();
		} finally {
		}
	}

}
